package webapp.backOffice.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modele.utilisateur.Entreprise;
import modele.utilisateur.MembreSociete;
import webapp.tools.Auth;
import dao.utilisateur.EntrepriseDAO;
import dao.utilisateur.MembreSocieteDAO;


@WebServlet("/admin/membres")
public class MembreControler extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
        if(!Auth.isAdmin(request.getSession()))
        {
            response.sendRedirect(request.getContextPath() + "/admin");
        }
        else
        {
        	
			String action = request.getParameter("action");
			String id = request.getParameter("userid");
			String entrepriseid = request.getParameter("entrepriseid");
			
			if(action != null)
			{
				if(action.equals("edit") && id != null)
				{
					request.setAttribute("user", _getUser(Long.parseLong(id)));
		            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewBackoffice/membre.jsp");
		            dispatcher.forward(request, response);
				}
				else if(action.equals("insert") && entrepriseid != null)
				{
		            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewBackoffice/membre.jsp");
		            dispatcher.forward(request, response);
				}	
				else if(action.equals("delete") && id != null)
				{
					_delete(Long.parseLong(id));
					response.sendRedirect(request.getContextPath() + "/admin/membres");
				}
			}
			else
			{
				request.setAttribute("users", _getInvestisseurs());
	            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewBackoffice/membreliste.jsp");
	            dispatcher.forward(request, response);
				
			}
        }
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*if(!Auth.isAdmin(request.getSession()))
        {
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewBackoffice/login.jsp");
            dispatcher.forward(request, response);
        }
        else*/
		{
			String action = request.getParameter("action");
			String id = request.getParameter("userid");
			
			if(action != null)
			{
				if(action.equals("edit"))
				{
					_edit(request);
					response.sendRedirect(request.getContextPath() + "/admin/membres");
				}

				else if(action.equals("insert"))
				{
					_create(request);
					response.sendRedirect(request.getContextPath() + "/admin/membres");
				}
			}
		}
	}
	
	
	private List<MembreSociete> _getInvestisseurs()
	{
		return new MembreSocieteDAO().retrieveAll();
	}
	
	private void _delete(long id)
	{
		MembreSocieteDAO dao = new MembreSocieteDAO();
		MembreSociete membre = dao.retrieve(id);
		if(membre != null)
			dao.delete(membre);
	}
	
	
	private MembreSociete _getFromForm(HttpServletRequest request, MembreSociete user)
	{
		Entreprise entr = new EntrepriseDAO().retrieve(Long.parseLong(request.getParameter("entrepriseid")));
		
		user.setEntreprise(entr);
		user.setAdresse(request.getParameter("adresse"));
		user.setCodePostal(request.getParameter("cp"));
		user.setDateNaissance(request.getParameter("dateNaissance"));
		user.setEmail(request.getParameter("email"));
		user.setEtatProfil(true);
		user.setLogin(request.getParameter("login"));
		user.setMdp(request.getParameter("mdp"));
		user.setNom(request.getParameter("nom"));
		user.setPrenom(request.getParameter("prenom"));
		user.setTelephone(request.getParameter("tel"));
		user.setVille(request.getParameter("ville"));
		return user;
	}
	
	private void _edit(HttpServletRequest request)
	{
		MembreSociete membre = _getUser(Long.parseLong(request.getParameter("userid")));
		_getFromForm(request, membre);
		new MembreSocieteDAO().update(membre);
	}
	
	private void _create(HttpServletRequest request)
	{
		MembreSociete membre = new MembreSociete();
		_getFromForm(request, membre);
		new MembreSocieteDAO().create(membre);
	}
	
	private MembreSociete _getUser(long id)
	{
		return new MembreSocieteDAO().retrieve(id);
	}

}
